package com.eagle.videoboot.controller;

import com.eagle.videoboot.domain.vo.ResultVO;
import com.eagle.videoboot.exception.ServiceException;
import com.eagle.videoboot.utils.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
@Slf4j
public class GlobalControllerExceptionHandler {

	@ExceptionHandler(Exception.class)
	@ResponseBody
	public ResultVO<?> handleGenericException(Exception e) {
		log.error(e.getMessage(), e);
		return ResultVOUtil.error(e.getMessage());
	}

	@ExceptionHandler(ServiceException.class)
	@ResponseBody
	public ResultVO<?> handleBizException(Exception e) {
		log.error(e.getMessage(), e);
		ServiceException se = (ServiceException) e;
		if(StringUtils.isNotBlank(se.getErrCode())){
			return ResultVOUtil.error(se.getErrCode(),se.getMessage());
		}
		return ResultVOUtil.error(e.getMessage());
	}
}
